﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Faq
{
    public class Support:BaseFaq
    {
        #region "Constructors"
        public Support() { }

        public Support(int supportID, DateTime addedDate, string addedBy, string title, string yahoo, string skype, bool showYahoo, bool showSkype, bool visible)
        {
            this.ID = supportID;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Title = title;
            this.Yahoo = yahoo;
            this.Skype = skype;
            this.ShowYahoo = showYahoo;
            this.ShowSkype = showSkype;
            this.Visible = visible;
        }
        #endregion

        #region "Public Properties"

             public string Title { get; set; }

        public string Yahoo { get; set; }

        public string Skype { get; set; }

        public bool ShowYahoo { get; set; }

        public bool ShowSkype { get; set; }

        public bool Visible { get; set; }
        #endregion

        #region Get Support From SupportsDetail
        private static Support GetSupportFromSupportDetail(SupportsDetails record)
        {
            if (record == null)
                return null;
            else
                return new Support(record.SupportID, record.AddedDate, record.AddedBy, record.Title, record.Yahoo, record.Skype, record.ShowYahoo, record.ShowSkype, record.Visible);
        }

        private static List<Support> GetSupportFromListSupportDetail(List<SupportsDetails> recordset)
        {
            List<Support> Support = new List<Support>();
            foreach (SupportsDetails record in recordset)
                Support.Add(GetSupportFromSupportDetail(record));
            return Support;
        }
        #endregion

        #region Method
        public static Support GetSupport(int SupportID)
        {
            Support Support = null;
            string key = "Support_" + SupportID.ToString();
            if (BaseFaq.Settings.EnableCaching && BizObject.Cache[key] != null)
                Support = (Support)BizObject.Cache[key];
            else
            {
                Support = GetSupportFromSupportDetail(SiteProvider.Faq.SupportGet(SupportID));
                BaseFaq.CacheData(key, Support);
            }
            return Support;
        }

        public static List<Support> ListSupportVisible()
        {
            List<Support> Support = null;
            string key = "Support_visible";

            if (BaseFaq.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                Support = (List<Support>)BizObject.Cache[key];
            }
            else
            {
                List<SupportsDetails> recordset = SiteProvider.Faq.SupportListVisible();
                Support = GetSupportFromListSupportDetail(recordset);
                BaseFaq.CacheData(key, Support);
            }
            return Support;
        }

        public static List<Support> ListSupport()
        {
            List<Support> Support = null;
            string key = "Support";
            if (BaseFaq.Settings.EnableCaching && BizObject.Cache[key] != null)
                Support = (List<Support>)BizObject.Cache[key];
            else
            {
                List<SupportsDetails> recordset = SiteProvider.Faq.SupportList();
                Support = GetSupportFromListSupportDetail(recordset);
                BaseFaq.CacheData(key, Support);
            }
            return Support;
        }

        public static int InsertSupport(string title, string yahoo, string skype, bool showyahoo, bool showskype, bool visible)
        {
            yahoo = Helpers.ConvertNullToEmptyString(yahoo);
            skype = Helpers.ConvertNullToEmptyString(skype);
            SupportsDetails record = new SupportsDetails(0, DateTime.Now,BizObject.CurrentUserName,title,yahoo,skype,showyahoo,showskype,visible);
            int ret = SiteProvider.Faq.InsertSupport(record);
            BizObject.PurgeCacheItems("Support");
            return ret;
        }

        public static bool UpdateSupport(int id, string title, string yahoo, string skype, bool showyahoo, bool showskype, bool visible)
        {
            yahoo = Helpers.ConvertNullToEmptyString(yahoo);
            skype = Helpers.ConvertNullToEmptyString(skype);
            SupportsDetails record = new SupportsDetails(id, DateTime.Now, BizObject.CurrentUserName, title, yahoo, skype, showyahoo, showskype, visible);
            bool ret = SiteProvider.Faq.UpdateSupport(record);
            BizObject.PurgeCacheItems("Support");
            return ret;
        }

        public static bool DeleteSupport(int id)
        {
            bool ret = SiteProvider.Faq.DeleteSupport(id);
            BizObject.PurgeCacheItems("Support");
            return ret;
        }
        #endregion
    }
}
